package com.agang.community.dto;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：冀阿刚
 * @date ：Created in 2020/1/11 0011 18:58
 */
@Data
public class PaginationDTO {

    private List<QuestionDTO> question;

    private boolean showPrevious;  //向前按钮

    private boolean showFirstPage; //第一页按钮

    private boolean showNext;

    private boolean showEndPage;

    private Integer page; //当前页

    private List<Integer> pages = new ArrayList<>();

    private Integer totalPage;

    public void setPagination(Integer totalCount, Integer page, Integer size) {


        if (totalCount % size == 0){
            totalPage = totalCount / size;
        }else {
            totalPage = totalCount / size +1;
        }

        if (page < 1) {
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }
        this.page = page;

        pages.add(page);
        for (int i=1; i<=3; i++){
            if (page - 1 >0){
                pages.add(0,page -i);
            }
            if (page + 1 <= totalPage){
                pages.add(page + i);
            }
        }

        //是否展示上一页
        if (page == 1){
            showPrevious = false;
        }else {
            showPrevious = true;
        }

        //是否展示下一页
        if (page == totalPage){
            showNext = false;
        }else {
            showNext = true;
        }

        //是否展示首页
        if (pages.contains(1)){ //如果包含1就不展示
            showFirstPage = false;
        }else {
            showFirstPage = true;
        }

        //是否展示末页
        if (pages.contains(totalCount)){
            showEndPage = false;
        }else {
            showEndPage = true;
        }
    }
}
